<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/mobile/include/taglib.jsp"%>
<html>
<head>
	<title>活动海报</title>
	<meta name="decorator" content="default_mb"/>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element.parent());
					}
				}
			});
		});
		
		function showPhoto(path){
			$("#picUrl").val(path);
			$("#photo").css("display", "block");
			var imageSrc = '${ctx}/download?filePath='+path;
			$("#photo").attr("src", imageSrc);
		}
		
		function showPopupDialog(){
			$("#popupDialog").popup("open");
			$("input[type='file']").show();
		}
		
		function uploadFun(){
		    if($("#uploadFile").val()=="") alert("请选择上传的图片.")
			else{
				showLoader("上传中...");
				//上传文件
			    $.ajaxFileUpload({
			    	type: "post",
			        url:'${ctx}/servlet/phonegapUp',//处理图片脚本
			        secureuri :false,
			        fileElementId :'uploadFile',//file控件id
			        dataType : 'json',
			        success : function (data){
			        	hideLoader();
			        	data = eval(data);
			            if(data.error == 0){
			            	alert("上传成功.");
			            	var url = data.url;
			            	showPhoto(url);
			            	$("#popupDialog").popup("close");
			            	$("input[type='file']").hide();
			            }else{
			            	alert(data.message);
			            }
			        },
			        error: function(data, e){
			            alert(e);
			        }
				});
			}
		}
	</script>
</head>
<body>
	<div data-role="popup" id="popupDialog" data-overlay-theme="b" data-theme="a" data-dismissible="false">
	    <div data-role="header" data-theme="a">
	    <h1>图片上传</h1>
	    </div>
	    <div role="main" class="ui-content">
	        <div class="file-box">
		     	<form name="inputForm">
			          <input type="file" accept="image/*" name="uploadFile" id="uploadFile"  />
			          <a href="#" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-a" onclick="uploadFun()">上传</a>
			          <a href="#" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-a" data-rel="back">取消</a>
		      	</form>
		    </div>
	    </div>
	</div>
	<form:form id="inputForm" modelAttribute="activityPoster" action="${ctx}/ivm/activity/poster/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<form:hidden path="merchant.id"/>
		<tags:mbmessage content="${message}"/>
		<div class="control-group">
			<label class="control-label">活动名称:</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="200" class="required"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">海报封面:</label>
			<div class="controls">
				<a href="#" onclick="showPopupDialog()" data-rel="popup" data-position-to="window" data-transition="pop" data-role="button" data-inline="true" data-icon="action" style="margin-top: 5px;">上传图片</a><br/><span style="color: red;">（建议尺寸为640x960）</span>
				<form:hidden path="picUrl" />
				<div class="actphoto" style="margin-top: 10px">
					<c:if test="${activityPoster.id!=null}">
						<img id="photo" src="${ctx}/download?filePath=${activityPoster.picUrl }" style="width:200px; height:300px;" />
					</c:if>
					<c:if test="${activityPoster.id==null}">
						<img id="photo" src="" style="width:200px; height:300px;" />
					</c:if>
				</div>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">是否开放:</label>
			<div class="controls">
				<form:radiobuttons path="openState" items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false" class="required" />
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="ivm:activity:poster:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
		</div>
	</form:form>
</body>
</html>
